﻿using System;
using System.Data.Linq;
using System.Linq;
using WebUI.DAL;
using System.Collections.Generic;

namespace WebUI.BL.Event
{
    public class EventBL
    {
        protected void clseventsInsert(tbl_Event eventss)
        {
            using (AdlerContextDataContext adlerContextDb = new AdlerContextDataContext())
            {
                try
                {
                    tbl_Event events = new tbl_Event();
                    events.EventName = eventss.EventName;
                    events.EventLink = eventss.EventLink;
                    events.EventShortContent = eventss.EventShortContent;
                    events.EventDetailContent = eventss.EventDetailContent;
                    events.CreatedDate = DateTime.Now;
                    events.CreatedUser = eventss.CreatedUser;
                    events.Status = eventss.Status;

                    adlerContextDb.tbl_Events.InsertOnSubmit(events);
                    adlerContextDb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    adlerContextDb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    adlerContextDb.SubmitChanges();
                }
            }
        }

        protected void clseventsModify(tbl_Event eventss)
        {
            using (AdlerContextDataContext adlerContextDb = new AdlerContextDataContext())
            {
                try
                {
                    var events = adlerContextDb.tbl_Events.Single(d => d.EventNo == eventss.EventNo);
                    events.EventName = eventss.EventName;
                    events.EventLink = eventss.EventLink;
                    events.EventShortContent = eventss.EventShortContent;
                    events.EventDetailContent = eventss.EventDetailContent;
                    events.CreatedDate = DateTime.Now;
                    events.CreatedUser = eventss.CreatedUser;
                    events.Status = eventss.Status;

                    adlerContextDb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    adlerContextDb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    adlerContextDb.SubmitChanges();
                }
            }
        }

        protected void clseventsDelete(tbl_Event eventss)
        {
            using (AdlerContextDataContext adlerContextDb = new AdlerContextDataContext())
            {
                try
                {
                    var events = adlerContextDb.tbl_Events.Single(d => d.EventNo == eventss.EventNo);
                    events.Status = eventss.Status;

                    adlerContextDb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    adlerContextDb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    adlerContextDb.SubmitChanges();
                }
            }
        }

        // Show Event.
        public List<MODEL_SP_ADMIN_SEARCH_EVENT> Get_Admin_CouponSerials(string eventname, string datefrom, string dateto, string status)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                int index = 1;
                //var query = from n in contextdb.SP_ADMIN_SEARCH_EVENT(eventname, datefrom, dateto, status)
                //            select new MODEL_SP_ADMIN_SEARCH_EVENT
                //            {
                //                Index = index++,
                //                EventNo = n.EventNo,
                //                EventName = n.EventName,
                //                EventLink = n.EventLink,
                //                EventShortContent = n.EventShortContent,
                //                EventDetailContent = n.EventDetailContent,
                //                CreatedDate = n.CreatedDate,
                //                CreatedUser = n.CreatedUser,
                //                ModifiedDate = n.ModifiedDate,
                //                UpdatedUser = n.UpdatedUser,
                //                Status = n.Status
                //            };
                //return query.ToList();
                return null;
            }
        }

        public tbl_Event GetCouponSerialByCouponno(int eventno)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                return contextdb.tbl_Events.Single(d => d.EventNo == eventno);
            }
        }
    }
}
